/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>

#include "dal/domain_participant.h"
#include "dal_fastdds/entity_impl/DalDomainParticipantImpl.hpp"
#include "dal_fastdds/converter/GuidConverter.hpp"
#include "dal_fastdds/converter/WriteParamsConverter.hpp"

#include <fastdds/dds/domain/DomainParticipantFactory.hpp>

using namespace eprosima::fastrtps::rtps;

TEST(test_convert_guid_from_fastdds_to_dal, convert_write_params)
{
    // clang-format off
    uint8_t uchs[16] = {
        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
    // clang-format on
    GUID_t f_guid;
    memcpy(f_guid.guidPrefix.value, uchs, 12);
    memcpy(f_guid.entityId.value, uchs + 12, 4);

    WriteParams f_params;
    dal_write_params_t d_params = { };
    f_params.sample_identity().writer_guid(f_guid);
    f_params.sample_identity().sequence_number().high = 111222;
    f_params.sample_identity().sequence_number().low = 333444;

    f_params.related_sample_identity().writer_guid(f_guid);
    f_params.related_sample_identity().sequence_number().high = 111222;
    f_params.related_sample_identity().sequence_number().low = 333444;

    WriteParamsConverter::GetInstance()->ConvertWriteParamsFromFastddsToDal(&f_params, &d_params);

    EXPECT_EQ(0,
              memcmp(d_params.sample_identity.writer_guid.guidprefix.value, uchs, GUIDPREFIX_SIZE));
    EXPECT_EQ(0,
              memcmp(d_params.sample_identity.writer_guid.entity_id.value,
                     uchs + GUIDPREFIX_SIZE,
                     ENTITYID_SIZE));
    EXPECT_EQ(d_params.sample_identity.sequence_number.high, 111222);
    EXPECT_EQ(d_params.sample_identity.sequence_number.low, 333444);
}

TEST(test_convert_guid_from_dal_to_fastdds, convert_write_params)
{
    // clang-format off
    uint8_t uchs[16] = {
        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
    // clang-format on
    dal_write_params_t d_params = { };
    WriteParams f_params;
    memcpy(d_params.sample_identity.writer_guid.guidprefix.value, uchs, GUIDPREFIX_SIZE);
    memcpy(d_params.sample_identity.writer_guid.entity_id.value,
           uchs + GUIDPREFIX_SIZE,
           ENTITYID_SIZE);
    d_params.sample_identity.sequence_number.high = 112233;
    d_params.sample_identity.sequence_number.low = 445566;

    WriteParamsConverter::GetInstance()->ConvertWriteParamsFromDalToFastdds(&d_params, &f_params);

    EXPECT_EQ(
        0,
        memcmp(f_params.sample_identity().writer_guid().guidPrefix.value, uchs, GUIDPREFIX_SIZE));
    EXPECT_EQ(0,
              memcmp(f_params.sample_identity().writer_guid().entityId.value,
                     uchs + GUIDPREFIX_SIZE,
                     ENTITYID_SIZE));
    EXPECT_EQ(f_params.sample_identity().sequence_number().high, 112233);
    EXPECT_EQ(f_params.sample_identity().sequence_number().low, 445566);
}
